package com.jky.stmall.manger.mapper;

import com.jky.stmall.manger.entity.GoodsPic;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface GoodsPicMapper {

    @Insert({"<script>",
            "insert into goods_pic",
            "(url,goods_Id)",
            "value",
            "<foreach collection='pics' item ='pic' separator=','>",
            "(#{pic.url},#{pic.goodsId})",
            "</foreach>",
            "</script>"})
    @Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "id")
    int insert(@Param("pics") List<GoodsPic> pics);

    //根据商品id删除图片
    @Delete("delete from goods_pic where goods_id=#{goodsId}")
    int deleteByGoodsId(Integer goodsId);

    @Update({"<script>",
            "update goods_pic",
            "<set>",
            "<if test='url!=null and url.length>0'>url=#{url},</if>",
            "<if test='goodsId!=null'>goods_id=#{goodsId},</if>",
            "</set>",
            "where id=#{id}",
            "</script>"})
    int update(GoodsPic goodsPic);

    @Select("select * from goods_pic where id=#{id}")
    GoodsPic selectById(Integer id);
    //根据商品ID查询图片
    @Select("select * from goods_pic where goods_id=#{goodsId}")
    List<GoodsPic> selectByGoodsId(Integer goodsId);


    @Select({"<script>",
            "select * from goods_pic",
            "<where>",
            "<if test='url!=null and url.length>0'> and url=#{url}</if>",
            "<if test='goodsId!=null and goodsId!=0'>and goods_id=#{goodsId}</if>",
            "<if test='goodsId!=null and goodsId==0'>and goods_id is null</if>",
            "</where>",
    "</script>"})
    List<GoodsPic> select(GoodsPic goodsPic);

}
